package com.example.jf.jfapp.adapter;

import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.ChooseImageBean;

import java.util.List;

import static com.example.jf.jfapp.PublishActivity.START_PICK_CODE;
import static com.example.jf.jfapp.bean.ChooseImageBean.TYPE_ADD;

/**
 * Created by jf on 2017/7/28.
 */

public class UpImageAdapter extends RecyclerView.Adapter<UpImageAdapter.ItemViewHolder> {
//    List<Uri> images; {
List<ChooseImageBean> mChooseImageBeens;

    public UpImageAdapter(List<ChooseImageBean> chooseImageBeens) {
        this.mChooseImageBeens = chooseImageBeens;
    }

    @Override
    public UpImageAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_up_image, parent, false);
        UpImageAdapter.ItemViewHolder itemViewHolder = new UpImageAdapter.ItemViewHolder(view);
        return itemViewHolder;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        ImageView iv_clear;

        public ItemViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            iv_clear = (ImageView) itemView.findViewById(R.id.iv_clear);
        }
    }

    boolean isEditStatu = false;//记录当前状态

    @Override
    public void onBindViewHolder(final UpImageAdapter.ItemViewHolder holder, final int position) {
        ChooseImageBean chooseImageBean = mChooseImageBeens.get(position);

        //判断item的类型

        //选中图片
        if(chooseImageBean.getType() == ChooseImageBean.TYPE_IMAGE) {
            showImage(holder,chooseImageBean,position);
        }else {//+ 号
            showAdd(holder);
        }
    }

    private void showAdd(final UpImageAdapter.ItemViewHolder holder){
        //这里的holder 可能是之前缓存下来的
//        holder.iv.setBackgroundResource(R.drawable.ic_add_black);
        holder.iv.setImageResource(R.drawable.ic_add_black);
        holder.iv_clear.setVisibility(View.GONE);

        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                ((Activity)holder.itemView.getContext()).startActivityForResult(intent, START_PICK_CODE);
            }
        });
    }


    //显示选中的图片
    private void showImage(UpImageAdapter.ItemViewHolder holder, ChooseImageBean chooseImageBean, final int position){
        Glide.with(holder.itemView.getContext())
                .load(chooseImageBean.getUri())
                .error(R.mipmap.ic_error)//失败的显示的图片
//                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE 缓存
                //三级缓存
                //1. 内存缓存,保存到运行内存中，ram
                //2. 保存到本地，sd卡中
                //3. 保存到网络中
                .into(holder.iv);
        if (isEditStatu) {
            holder.iv_clear.setVisibility(View.VISIBLE);
            // X 的点击事件
            holder.iv_clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    isEditStatu = false;//修改编辑状态
                    mChooseImageBeens.remove(position);

                    boolean flag = false;//记录是否有+号
                    for (int i = 0; i < mChooseImageBeens.size(); i++) {
                        if(mChooseImageBeens.get(i).getType() == TYPE_ADD){//有+号
                            flag = true;
                            break;
                        }
                    }

                    //集合中存在+号了
                    if(flag){

                    }else{//不存在，添加+号
                        mChooseImageBeens.add(new ChooseImageBean(TYPE_ADD,null));
                    }

                    notifyDataSetChanged();
                }
            });
        } else {
            holder.iv_clear.setVisibility(View.GONE);
        }
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果处于编辑状态
                if (isEditStatu) {
                    isEditStatu = false;
                    notifyDataSetChanged();
                    return;
                } else {

                }
            }
        });


        holder.iv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                isEditStatu = !isEditStatu;
                notifyDataSetChanged();
                //Toast.makeText(holder.iv.getContext(), "长按啦", Toast.LENGTH_SHORT).show();

                return true;
            }
        });


    }




    @Override
    public int getItemCount() {
        return mChooseImageBeens.size();
    }
}
